package day0717;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Random;

public class Test5 {
    public static void main(String[] args) {

        JFrame win=new JFrame("羊了个羊");

        JPanel panel=new JPanel();


        panel.setLayout(null);//布局方式为null//取消排版方式

        JLabel  label=new JLabel(new ImageIcon("images/背景.jpg"));
        label.setSize(480,800);
        label.setLocation(0,0);
        panel.add(label);

        String[] a=new String[]{"刷子","剪刀","南瓜","奶瓶","帽子","干草","手套","月饼","树","树桩","栗子",
                "毛线","水桶","火堆","白菜","耙子","苹果","草","萝卜","蜜蜂","酒坛","铃铛","面具","饭团"};
        ArrayList<JButton> btns=createBtns(a);//144//24*6
        for(int i=0;i<42; i++) {
            int index=(int)(Math.random()*i);
            JButton btn = btns.get(i);
            btn.setSize(59, 66);
            for (int j = 0; j < 8; j++) {
                /*if (59*j<480){
                    btn.setLocation(59*i, 66);///////////////////
                }else if (59*j>480){
                    btn.setLocation(59*i, 66*i);///////////////////
                }*/
            }
            btn.setLocation(i%7*59  +20 ,   i/7*66 +40);
            panel.add(btn, 0);//0表示将btn添加到面板panel的最顶层。
        }


        win.add(panel);
        win.setSize(480,800);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setLocationRelativeTo(null);
        win.setVisible(true);
    }

    private static ArrayList<JButton> createBtns(String[] a) {
        ArrayList<JButton> arrayList=new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 24; j++) {
                JButton btn=new JButton(new ImageIcon("images/"+a[j]+".png"));
                //
                arrayList.add(btn);
            }
        }
        return arrayList;
    }


}
